/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.loja.model.entity.transacao;

import br.com.loja.model.entity.Switchable;
import java.math.BigDecimal;
import java.util.Calendar;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import br.com.loja.model.entity.conta.Conta;
import br.com.loja.model.entity.conta.Operacao;
import br.com.loja.view.swing.model.FormField;
import br.com.loja.view.swing.model.IgnoreTableModel;
import java.io.Serializable;

/**
 *
 * @author David Luiz
 */
@Entity
@Table(name = "transacao")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Transacao implements Switchable, Serializable {

    public static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long idTransacao;
    @ManyToOne(cascade = CascadeType.ALL)
    private Conta remetente;
    @ManyToOne(cascade = CascadeType.ALL)
    private Conta favorecida;
    private BigDecimal valor;
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar dataTransacao;
    private String historico;

    @FormField(visible = false)
    @IgnoreTableModel
    private boolean dead;

    public final Transacao prepararTransacao() {

        Operacao operacaoDebito = new Operacao();
        operacaoDebito.setHistorico(this.historico);
        operacaoDebito.setValor(this.valor.negate());

        Operacao operacaoCredito = new Operacao();
        operacaoCredito.setHistorico(this.historico);
        operacaoCredito.setValor(this.valor);

        if (remetente != null) {
            remetente.getOperacoes().add(operacaoDebito);
        }

        if (favorecida != null) {
            favorecida.getOperacoes().add(operacaoCredito);
        }

        return this;

    }

    /**
     * @return the idTransacao
     */
    public Long getIdTransacao() {
        return idTransacao;
    }

    /**
     * @param idTransacao the idTransacao to set
     */
    public void setIdTransacao(Long idTransacao) {
        this.idTransacao = idTransacao;
    }

    /**
     * @return the remetente
     */
    public Conta getRemetente() {
        return remetente;
    }

    /**
     * @param remetente the remetente to set
     */
    protected void setRemetente(Conta remetente) {
        this.remetente = remetente;
    }

    /**
     * @return the favorecida
     */
    public Conta getFavorecida() {
        return favorecida;
    }

    /**
     * @param favorecida the favorecida to set
     */
    protected void setFavorecida(Conta favorecida) {
        this.favorecida = favorecida;
    }

    /**
     * @return the valor
     */
    public BigDecimal getValor() {
        return valor;
    }

    /**
     * @param valor the valor to set
     */
    public void setValor(BigDecimal valor) {
        this.valor = valor;
    }

    /**
     * @return the dataTransacao
     */
    public Calendar getDataTransacao() {
        return dataTransacao;
    }

    /**
     * @param dataTransacao the dataTransacao to set
     */
    public void setDataTransacao(Calendar dataTransacao) {
        this.dataTransacao = dataTransacao;
    }

    public String getHistorico() {
        return this.historico;
    }

    public void setHistorico(String historico) {
        this.historico = historico;
    }

    @Override
    public boolean isDead() {
        return dead;
    }

    @Override
    public void setDead(boolean dead) {
        this.dead = dead;
    }

}
